package ip

import (
	"fmt"
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/text/gstr"
)

//GetAddressByIp 获取IP解析地址
func GetAddressByIp(ip string) string {
	if ip == "" {
		return ""
	}

	if ip == "[::1]" || ip == "127.0.0.1" {
		return "本机IP"
	}

	url := "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip
	bytes := ghttp.GetBytes(url)
	json, err := gjson.DecodeToJson(bytes)
	if err != nil {
		return ""
	}
	if json.GetInt("code") == 0 {
		return fmt.Sprintf("%s %s %s %s",
			json.GetString("data.country"),
			json.GetString("data.region"),
			json.GetString("data.city"),
			json.GetString("data.county"))
	} else {
		return ""
	}
}

//GetDriverType 获取设备类型
func GetDriverType(userAgent string) (dp string) {
	if gstr.Contains(userAgent, "okhttp") || gstr.Contains(userAgent, "ios") {
		return "app"
	}
	return "web"
}
